package com.asa.chart;


import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.swing.*;

public class Test extends JFrame implements ActionListener {
	JButton button = new JButton("启动");
	JButton button1 = new JButton("外部类启动");
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Runnable tr = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Test();
			}
		};
		javax.swing.SwingUtilities.invokeLater(tr);
	}

	public Test() {
		super("测试窗口");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(800, 600);
		Container c = this.getContentPane();
		GridBagLayout layout = new GridBagLayout();
		this.setLayout(layout);
		GridBagConstraints g = new GridBagConstraints();
		cp = new CurvePanel();
		c.add(cp);
		g.gridx = 0;
		g.gridy = 0;
		g.gridwidth = 8;
		g.gridheight = 9;
		g.weightx = 0.8;
		g.weighty = 0.9;
		g.fill = GridBagConstraints.BOTH;
		layout.setConstraints(cp, g);

		c.add(button);
		g.gridx = 0;
		g.gridy = 9;
		g.gridwidth = 1;
		g.gridheight = 1;
		g.weightx = 0.1;
		g.weighty = 0.1;
		g.fill = GridBagConstraints.NONE;
		layout.setConstraints(button, g);
		button.addActionListener(this);

		c.add(button1);
		g.gridx = 1;
		g.gridy = 9;
		g.gridwidth = 1;
		g.gridheight = 1;
		g.weightx = 0.1;
		g.weighty = 0.1;
		g.fill = GridBagConstraints.NONE;
		layout.setConstraints(button1, g);
		button1.addActionListener(this);
		this.setVisible(true);
		
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object obj = e.getSource();
		if (obj.equals(button)) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						while (true) {
							cp.setDrawLine();
							Thread.sleep(1000);
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
		} else if (obj.equals(button1)) {
			StartThread st = new StartThread();
			new Thread(st).start();
		}
	}

	private static CurvePanel cp;


	public static CurvePanel getCp() {
		return cp;
	}

	public void setCp(CurvePanel cp) {
		this.cp = cp;
	}
}

class StartThread implements Runnable {
	CurvePanel cp = Test.getCp();

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while (true) {
				cp.setDrawLine();
				Thread.sleep(1000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

// 画曲线
class CurvePanel extends JPanel {
	List<Integer> values;

	public CurvePanel() {
		values = Collections.synchronizedList(new ArrayList<Integer>());
	}

	public void addValue(int value) {
		values.add(value);
		System.out.println(values);
	}

	public void setDrawLine() {
		Random rand = new Random();
		addValue(rand.nextInt(400));
		repaint();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		for (int i = 0; i < values.size() - 1; i++) {
			g2d.setColor(Color.red);
			g2d.drawLine(5 * i, values.get(i), 5 * (i + 1), values.get(i + 1));
		}
	}
}
